﻿Type=Activity
Version=5.8
ModulesStructureVersion=1
B4A=true
@EndOfDesignText@
#Region  Activity Attributes 
	#FullScreen: False
	#IncludeTitle: True
#End Region

Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.
	Public sDir=File.DirRootExternal,sName="tmpHeader.jpg" As String
End Sub

Sub Globals
	'These global variables will be redeclared each time the activity is created.
	'These variables can only be accessed from this module.
	
	Private clvmyinfos As clsCustomListView
	Private itemname As Label
	Private itemvalue As Label
	Private ivrightarr As ImageView
	Private myheaderriv As RIV
	'Private ion As Object
	Private gf As GalleryFinal
	Private mi As myInfo
	Private thirdlogin As ThirdLogin
End Sub

Sub Activity_Create(FirstTime As Boolean)
	'Do not forget to load the layout file created with the visual designer. For example:
	Activity.LoadLayout("laymodifymyinfo")
	initviews
End Sub
Private Sub initviews
	gf.Init("gf",comm.S_GalleryFinalTheme)
	mi=g_store.conf.Get("myinfo")
	thirdlogin.init("tl",comm.SHARE_APPKEY,comm.getShareSdkCfgList,True)
	'mitmp=mi
	'构建列表
	Dim nh As Int=60dip,nw As Int=100%x
	clvmyinfos.dividerHeight=1dip
	clvmyinfos.Add(CreateItemHeader(nw,nh),65dip,0)
	clvmyinfos.Add(CreateItemCommon("昵称",mi.nickname,nw,nh,True),nh,1)
	clvmyinfos.Add(CreateItemCommon("性别",mi.sex,nw,nh,True),nh,2)
	clvmyinfos.Add(CreateItemCommon("年龄",""&mi.age,nw,nh,True),nh,3)
	clvmyinfos.Add(CreateItemCommon("年级",""&mi.grade,nw,nh,True),nh,4)
	clvmyinfos.Add(CreateItemCommon("学校",""&mi.school,nw,nh,True),nh,5)
	clvmyinfos.Add(CreateItemCommon("城市",""&mi.city,nw,nh,True),nh,6)
	clvmyinfos.Add(CreateItemCommon("常用地址",""&mi.address,nw,nh,True),nh,7)
	If mi.wxid.Length>5 Then
		clvmyinfos.Add(CreateItemCommon("绑定微信","已绑定",nw,nh,True),nh,11)	
	Else
		clvmyinfos.Add(CreateItemCommon("绑定微信","未绑定",nw,nh,True),nh,11)	
	End If
	If mi.qqid.Length>5 Then
		clvmyinfos.Add(CreateItemCommon("绑定QQ","已绑定",nw,nh,True),nh,12)
	Else
		clvmyinfos.Add(CreateItemCommon("绑定QQ","未绑定",nw,nh,True),nh,12)
	End If
	
	clvmyinfos.AddCustomDivider(5dip)
	clvmyinfos.Add(CreateItemCommon("手机号",mi.usertel,nw,nh,False),nh,8)
	clvmyinfos.Add(CreateItemCommon("修改密码","",nw,nh,True),nh,9)
	clvmyinfos.Add(CreateItemCommon("修改支付密码","",nw,nh,True),nh,10)
	clvmyinfos.AddCustomDivider(15dip)
	
End Sub
Sub tl_onCancel(action As Int,Platform As Object)
	comm.TL("tl_onCancel:"&action&" "&Platform)
	comm.T("取消了登录")
End Sub
Sub tl_onError(action As Int,platform As Object,errMsg As String)
	comm.L("tl_onError:"&action&" "&errMsg&" "&platform)
	Msgbox("错误:"&errMsg,"登录出错")
End Sub
Sub tl_onComplete(action As Int,platform As Object,res As Map)
	'comm.TL("tl_onComplete:"&action&" "&res&" "&platform)
'	For i=0 To res.Size-1
'		Log("key:"&res.GetKeyAt(i)&" val:"&res.GetValueAt(i))
'	Next
	Dim uid As String=res.Get("userid")
	Dim pname As String=res.Get("pname")
	pname=pname.ToLowerCase
	reqBindAccount(uid,pname)
End Sub
Private Sub reqBindAccount(uid As String,pn As String)
	comm.showProgress("正在提交")
	Dim m As Map=CreateMap("userid":uid,"pname":pn,"is_teach":"0")
	ParseHelper.doPostReqMap(Me,"bind",comm.getApi(comm.apiIdx.REQBINDTHIRDACCOUNT),m,"")
End Sub
Sub btnsubmit_Click
	reqModifySubmit
End Sub
Private Sub reqModifySubmit
	Dim jsg As JSONGenerator
	Dim m,m2 As Map
	m.Initialize
	m2.Initialize
	m2.Put("address",mi.address)
	m2.Put("city",mi.city)
	m2.Put("autograph",mi.commit)
	m2.Put("sex",mi.sex)
	m2.Put("nianji",mi.grade)
	m2.Put("image",mi.headimg)
	m2.Put("username",mi.nickname)
	m2.Put("hy_school",mi.school)
	m2.Put("age",mi.age)
	m.Put("data",m2)
	jsg.Initialize(m)
	Dim ar() As String=Array As String("data",jsg.ToString)
	comm.showProgress("正在提交")
	ParseHelper.doPostReqArr(Me,"submit",comm.getApi(comm.apiIdx.MODIFYMYINFO),ar,"")
End Sub
Private Sub CreateItemCommon(str As String,val As String,w As Int,h As Int,hasArr As Boolean) As Panel
	Dim p As Panel
	p.Initialize("")
	Activity.AddView(p,0,0,w,h)
	p.LoadLayout("itemmodifymyinfocommon")
	ivrightarr.Visible=hasArr
	p.RemoveView
	itemname.Text=str
	itemvalue.Text=val
	Return p
End Sub
Private Sub CreateItemHeader(w As Int,h As Int) As Panel
	Dim p As Panel
	p.Initialize("")
	Activity.AddView(p,0,0,w,h)
	p.LoadLayout("itemmodifymyinfomyheader")
	itemname.Text="头像"
	p.RemoveView
	Return p
End Sub
Sub Activity_Resume
	If File.Exists(sDir,sName) Then
		'myheaderriv.ImageBitmap=LoadBitmapSample(sDir,sName,50,50)
		comm.loadimg_nocache("file://"&File.Combine(sDir,sName),myheaderriv)
		'myheaderriv.innerRIV.ImageBitmap=getRoundBmp(LoadBitmap(File.DirAssets,"test_myheader.jpg"),0,Colors.Transparent)
	Else
		If mi.headimg.Length>4 Then
			comm.loadimg(mi.headimg,myheaderriv)
		Else
			comm.loadimg(comm.S_TESTMYHEADER,myheaderriv)
		End If
	End If
	Dim curCity As String=g_store.conf.GetDefault("curcity","")
	If curCity.Length>0 Then
		mi.city=curCity
	End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
'#Region ShowPicker
''Sub ShowPicker
''   Dim i As Intent
''   i.Initialize("android.intent.action.RINGTONE_PICKER", "")
''   i.PutExtra("android.intent.extra.ringtone.TYPE", 1)
''   StartActivityForResult(i)
''End Sub
'Sub ShowImagePicker(dir As String,name As String)
'	Dim i As Intent
'	i.Initialize(i.ACTION_PICK, "")
'	i.SetType("image/*")
'	'i.AddCategory("android.intent.category.OPENABLE")
'	i.PutExtra("crop", "true")
'	i.PutExtra("output", comm.ParseUri("file://" & File.Combine(dir, name)))
'	i.PutExtra("aspectX", 1)
'    i.PutExtra("aspectY", 1)
''	i.PutExtra("outputX", aspectRatio.Width)
''	i.PutExtra("outputY", aspectRatio.Height)
'	StartActivity(i)
'End Sub
'Sub cropPicture(Directory As String, PictureName As String,  PictureName2 As String)
'    Dim i As Intent
'    i.Initialize("com.android.camera.action.CROP", comm.ParseUri("file://" & File.Combine(Directory, PictureName)))
'    i.SetType("image/*")
'    i.PutExtra("crop", "true")
'    i.PutExtra("aspectX", 1)
'    i.PutExtra("aspectY", 1)
'    i.PutExtra("output", comm.ParseUri("file://" & File.Combine(Directory, PictureName2)))
'    StartActivity(i)
'End Sub
'Sub StartActivityForResult(i As Intent)
'   Dim jo As JavaObject = GetBA
'   ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
'   jo.RunMethod("startActivityForResult", Array As Object(ion, i))
'End Sub
'Sub GetBA As Object
'   Dim jo As JavaObject
'   Dim cls As String = Me
'   cls = cls.SubString("class ".Length)
'   jo.InitializeStatic(cls)
'   Return jo.GetField("processBA")
'End Sub
'Sub ion_Event (MethodName As String, Args() As Object) As Object
'   If Args(0) = -1 Then 'resultCode = RESULT_OK
'     Dim i As Intent = Args(1)
'	 Log(i)
'     Dim jo As JavaObject = i
'     Dim uri As String = jo.RunMethod("getParcelableExtra", _
'       Array As Object("android.intent.extra.ringtone.PICKED_URI"))
'     Log(uri)
'   End If
'   Return Null
'End Sub
'#End Region
Sub gf_onHanlderFailure(requestCode As Int,errorMsg As String)
	ToastMessageShow("头像选择错误:"&errorMsg,True)
End Sub
Sub gf_onHanlderSuccess(reqeustCode As Int,resultList As List)
	If resultList.Size>0 Then
		File.Delete(sDir,sName)
		Dim tp As PhotoInfo=resultList.Get(0)
		File.Copy("",tp.PhotoPath,sDir,sName)
		Log("头像选择完毕")
'		comm.loadimg_nocache("file://"&tp.PhotoPath,myheaderriv)
		reqUploadImg(tp.PhotoPath)
	End If
End Sub
Sub JobDone(thj As HttpJob)
	ProgressDialogHide
	If thj.Success Then
		Dim str As String=thj.GetString
		Log(str)
		Dim nr As netRes=ParseHelper.getStatusData(str)
		If nr.nCode=200 Then
			Select thj.JobName
			Case "upimg"
				comm.TL("上传成功")
				Dim parser As JSONParser 
				parser.Initialize(str) 
				Dim root As Map = parser.NextObject 
				Dim data As String = root.Get("data") 
				Dim url As String = comm.combinPic(root.Get("url") )
				Dim status As String = root.Get("status") 
				mi.headimg=url
				g_store.conf.Put("myinfo",mi)
				comm.loadimg(url,myheaderriv)
				Try
					File.Delete(sDir,sName)
				Catch
					Log("delErr"&LastException.Message)
				End Try
			Case "submit"
				comm.TL("提交成功")
				Activity.Finish
			Case "bind"
				comm.TL("账户绑定成功以后你可以用第三方账号登录本app咯！")
			End Select
			
		Else
			comm.TL("错误:"&thj.JobName&nr.sData)
		End If
	Else
		comm.TL("网络错误:"&thj.JobName&thj.ErrorMessage)
	End If
	thj.Release
End Sub
Private Sub reqUploadImg(imgfile As String)
	comm.showProgress("正在上传头像")
	ParseHelper.doUploadFile(Me,"upimg",comm.getApi(comm.apiIdx.UPLOADIMG),imgfile)
End Sub
Sub clvmyinfos_ItemClick (Index As Int, Value As Object)
	Dim ind As InputDialog
	Dim cp As Panel=clvmyinfos.GetPanel(Value)
	Select Value
	Case 0
		'ShowImagePicker(sDir,sName)
		Dim lst As List
		lst.Initialize
		lst.AddAll(Array As String( "相机","相册"))
		Dim ret As Int=InputList(lst,"选择头像",-1)
		Select ret
			Case 0
				gf.openCamera(ret)
			Case 1
				gf.openGallerySingle(ret)
			Case Else
				ToastMessageShow("您取消了操作",False)
		End Select
	Case 1
		If ind.Show("请输入要设置的昵称","昵称","确定","取消","",Null)=DialogResponse.POSITIVE Then
			Try
				setItemVal(clvmyinfos.GetPanel(Value),ind.Input.Trim)
				mi.nickname=ind.Input.Trim
			Catch
				Log(LastException.Message)
			End Try
		End If
	Case 2'性别
		Dim lst As List
		lst.Initialize
		lst.AddAll(Array As String("男","女"))
		Dim rid As Int=InputList(lst,"选择性别",-1)
		If rid>-1 Then
			setItemVal(cp,lst.Get(rid))
			If rid=0 Then
				mi.sex=1
			Else
				mi.sex=2
			End If
		End If
	Case 3'age
		ind.InputType=ind.INPUT_TYPE_NUMBERS
		If ind.Show("请输入年龄","年龄","确定","取消","",Null)==DialogResponse.POSITIVE Then
			setItemVal(cp,ind.Input)
			mi.age=ind.Input
		End If
	Case 4'年级
		Dim lst As List=g_store.conf.Get(comm.S_NIANJI_FINDTEACHER)
		If lst.IsInitialized=False Then Return
		Dim rid As Int=InputList(lst,"选择所在年级",-1)
		If rid>-1 Then
			setItemVal(cp,lst.Get(rid))
			mi.grade=ind.Input
		End If
	Case 5'学校
		If ind.Show("请输入所在学校","学校","确定","取消","",Null)==DialogResponse.POSITIVE Then
			setItemVal(cp,ind.Input)
			mi.school=ind.Input
		End If
	Case 6
		CallSubDelayed2(actSelectCity,"reqloc",Me)
	Case 7
		'StartActivity(actAddressList)
		If ind.Show("请输入住地址","地址","确定","取消","",Null)==DialogResponse.POSITIVE Then
			setItemVal(cp,ind.Input)
			mi.address=ind.Input
		End If
	Case 9
		StartActivity(actModifyPassword)
	Case 10
		StartActivity(actModifyPayPwd)
	Case 11'微信绑定
		If mi.wxid.Length<5 Then'如果要换绑本地提示即可
				thirdlogin.LoginByWeixin
			Else
				comm.TL("已经绑定过了，如果要修改绑定账号请联系客服")
		End If
	Case 12'qq绑定
		If mi.qqid.Length<5 Then'如果要换绑本地提示即可
				thirdlogin.LoginByQQZone
			Else
				comm.TL("已经绑定过了，如果要修改绑定账号请联系客服")
		End If
	End Select
	DoEvents
	comm.hideIme
End Sub
Sub citySelected
	setItemVal(clvmyinfos.GetPanel(6),g_store.conf.Get("curcity"))
End Sub
Private Sub setItemVal(p As Panel,val As String)
	Dim lbl As Label
	For Each v As View In p.GetAllViewsRecursive
		Log(v.Tag)
		If "val".EqualsIgnoreCase(v.Tag) Then
			lbl=v
			lbl.Text=val
		End If
	Next
	Return
End Sub
Sub ivback_Click
	Activity.Finish
End Sub

'https://www.b4x.com/android/forum/threads/roundbitmapwithborder-inline-java-code.51848/#post-380801
'Public Sub getRoundBmp(bmin As Bitmap,borderWidth1 As Int,borderColor1 As Int) As Bitmap
'	Dim nativeMe As JavaObject
'	nativeMe.InitializeContext
'	Dim retbm As Bitmap=nativeMe.RunMethod("getRoundBitmap",Array(bmin,borderColor1, borderWidth1)) 
'	Return retbm
'End Sub
'Public Sub getSquareBmp(bmin As Bitmap,borderWidth2 As Int,borderColor2 As Int) As Bitmap
'	Dim nativeMe As JavaObject
'	nativeMe.InitializeContext
'    Dim bm2 As Bitmap = nativeMe.RunMethod("addSquareBorder",Array(bmin, borderWidth2, borderColor2))  
'	Return bm2
'End Sub
'#If Java
'
'import android.graphics.Bitmap;
'import android.graphics.Canvas;
'import android.graphics.Path;
'import android.graphics.Rect;
'import android.graphics.RectF;
'import android.graphics.drawable.BitmapDrawable;
'import android.graphics.drawable.Drawable;
'import android.graphics.Path.FillType;
'import android.graphics.Paint;
'import android.graphics.Color;
'import android.view.View;
'
'import android.annotation.SuppressLint;
'import android.content.Context;
'import android.graphics.BitmapShader;
'import android.graphics.Shader;
'import android.graphics.PorterDuffXfermode;
'import android.graphics.PorterDuff.Mode;
'
'
'public static Bitmap getRoundBitmap(Bitmap scaleBitmapImage, int borderColor, int borderWidth) {
'    int targetWidth = 1000;
'    int targetHeight = 1000;
'    int radius = Math.min((targetHeight - 5)/2, (targetWidth - 5)/2);  
'   
'    Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,
'            Bitmap.Config.ARGB_8888);
'
'    Canvas canvas = new Canvas(targetBitmap);
'    Path path = new Path();
'    path.addCircle(((float) targetWidth - 1) / 2,
'            ((float) targetHeight - 1) / 2,
'            (Math.min(((float) targetWidth), ((float) targetHeight)) / 2),
'            Path.Direction.CCW);
'
'    canvas.clipPath(path);
'   
'    Bitmap sourceBitmap = scaleBitmapImage;
'   
'    canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(),
'            sourceBitmap.getHeight()), new Rect(0, 0, targetWidth,
'            targetHeight), null);
'           
'           
'    Paint p = new Paint();                                             
'    p.setAntiAlias(true);                  
'    //    canvas.drawBitmap(sourceBitmap, 4, 4, p);                                     
'    p.setXfermode(null);                                               
'    p.setStyle(Paint.Style.STROKE);                                          
'    p.setColor(borderColor);                                           
'    p.setStrokeWidth(borderWidth);                                               
'    canvas.drawCircle((targetWidth / 2) , (targetHeight / 2) , radius, p);                             
'
'    return targetBitmap;
'}   
'   
'    public static Bitmap addSquareBorder(Bitmap bmp, int borderSize, int bordercolor) {
'           
'        Bitmap bmpWithBorder = Bitmap.createScaledBitmap(bmp, bmp.getWidth() + borderSize * 2, bmp.getHeight() + borderSize * 2, false);           
'           
'        Canvas canvas = new Canvas(bmpWithBorder);
'        canvas.drawColor(bordercolor);
'        canvas.drawBitmap(bmp, borderSize, borderSize, null);
'        return bmpWithBorder;
'    }   
'   
'   
'
'
'#End If
